package tq.com.tqteacher.mine.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import tq.com.tqcom.recyclerAdapter.BaseSectionAdapter;
import tq.com.tqcom.recyclerAdapter.declear.IRecyclerSection;
import tq.com.tqcom.vh.CommonViewHolder;
import tq.com.tqteacher.R;
import tq.com.tqteacher.mine.model.TQBuyInfoSection;

import static tq.com.tqteacher.mine.model.TQBuyInfoSection.VIEW_TYPE_YEAR;

public class TQBuyInfoAdapter extends BaseSectionAdapter {
    private Context mContext;



    public TQBuyInfoAdapter(Context pContext){
        mContext = pContext;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup pViewGroup, int pI) {
        if(pI == VIEW_TYPE_YEAR){
            return CommonViewHolder.sCommonViewHolder(mContext, R.layout.vh_buy_year_layout, pViewGroup).setHolderBinder(new CommonViewHolder.IViewHolder() {
                @Override
                public void bind(View pView, Object data) {
                    if(data == null || !(data instanceof TQBuyInfoSection.TQBuyInfoItem)){
                        return;
                    }

                    TextView lTextView = pView.findViewById(R.id.year);
                    TQBuyInfoSection.TQBuyInfoItem lInfoItem = (TQBuyInfoSection.TQBuyInfoItem) data;
                    lTextView.setText(lInfoItem.getYear());
                }
            });
        }

        View lView = LayoutInflater.from(mContext).inflate(R.layout.vh_bu_month_layout, pViewGroup, false);
        return new MonthBuyInfoViewHold(lView);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder pViewHolder, int pI) {
        TQBuyInfoSection.TQBuyInfoItem lInfoItem = (TQBuyInfoSection.TQBuyInfoItem) getItemWithPos(pI);
        if(lInfoItem.isYear() && pViewHolder instanceof CommonViewHolder){
            ((CommonViewHolder) pViewHolder).bindData(lInfoItem);
        }else if(pViewHolder instanceof MonthBuyInfoViewHold){
            ((MonthBuyInfoViewHold)pViewHolder).bindData(lInfoItem);
        }
    }



    class MonthBuyInfoViewHold extends RecyclerView.ViewHolder{
        private TextView mMonth;
        private TextView mLeft;
        private TextView mRight;

        public MonthBuyInfoViewHold(View pView){
            super(pView);

            mMonth = pView.findViewById(R.id.month);
            mLeft = pView.findViewById(R.id.txt_left);
            mRight = pView.findViewById(R.id.txt_right);

        }

        public void bindData(TQBuyInfoSection.TQBuyInfoItem pItem){
            if(pItem == null) return;

            mMonth.setText(pItem.getMonth());
            mLeft.setVisibility(pItem.isLeft() ? View.VISIBLE: View.INVISIBLE);
            mRight.setVisibility(pItem.isLeft() ? View.INVISIBLE : View.VISIBLE);

            mLeft.setText(String.valueOf(pItem.getCount()));
            mRight.setText(String.valueOf(pItem.getCount()));
        }


    }
}
